# @Time : 2020/7/20 10:11
# @Author : Fioman 
# @Phone : 13149920693

"""
cv.warpAffine()对图像进行旋转的时候,可以通过函数cv.getRotationMatrix2D()获取转换矩阵.
rotatedM = cv.getRotationMatrix2D(center,angle,scale)
opencv中如果是正值,表示逆时针旋转,负值表示顺时针旋转.
"""
import cv2 as cv
import numpy as np


def rotatedImg(img, angle, center=None, scale=1.0):
    if len(img.shape) <= 2:
        return "传入参数错误,非正确图像格式!"
    h, w = img.shape[:2]
    if center is None:
        center = (w / 2, h / 2)

    rotatedM = cv.getRotationMatrix2D(center, angle, scale)
    rotated = cv.warpAffine(img, rotatedM, (w, h))

    return rotated


if __name__ == '__main__':
    """
        将一个图像顺时针旋转45度,并且大小缩小为原来的1/4
    """
    img = cv.imread("lenaColor.png", cv.IMREAD_COLOR)
    h, w = img.shape[:2]
    rotatedM = cv.getRotationMatrix2D((w / 2, h / 2), -45, 0.25)
    rotated = cv.warpAffine(img, rotatedM, (w, h))
    cv.imshow("Rotated", rotated)
    cv.waitKey(0)

    scaled = 1.2
    rotated = rotatedImg(img,-90,scale=scaled)
    cv.imshow("Rotated2",rotated)
    cv.waitKey(0)
